package com.yang.flower.service.impl;

import cn.hutool.core.util.StrUtil;
        import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
        import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
        import com.yang.flower.entity.Menu;
        import com.yang.flower.mapper.MenuMapper;
        import com.yang.flower.service.MenuService;
        import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
        import java.util.stream.Collectors;

/**
 * @Description: MenuServiceImpl
 * @Author: Yang
 * @DateTime: 2022/12/9 17:04
 **/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Override
    public List<Menu> findMenus(String name) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(name)){
            queryWrapper.like(Menu::getName,name);
        }

        List<Menu> list =list(queryWrapper);
        List<Menu> parentNodes = list.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        for (Menu menu : parentNodes) {
            menu.setChildren(list.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }
        return parentNodes;
    }
}
